
public class string9 {

	public static void main(String[] args) throws Exception
	{
		String str = "123 12345 123 1234 123 123 12345 123456 1";
		StringBuilder strb = new StringBuilder(str);
		String buff = "";
		int ind1 = 0, ind2 = 0;
		for(int i = 0; i < strb.length(); i++)
		{	
			buff += strb.charAt(i); 
			if(strb.charAt(i) == ' ' || i == strb.length() - 1)
			{
				if(i != strb.length() - 1)
				{
					StringBuilder temp = new StringBuilder(buff);
					temp.deleteCharAt(temp.length() - 1);
					buff = temp.toString();
				}
				if(buff.length() < 5)
				{
					ind2 = i + 1;
					ind1 = ind2 - buff.length() - 1;
					strb.delete(ind1, ind2);
					i -= ind2 - ind1;
				}
				ind1 = ind2 = 0;
				buff = "";
			}
		}
		System.out.println(strb.toString());
	}

}
